﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jQueryDotNetLib.Controls
{
    [ParseChildren(ChildrenAsProperties=true, DefaultProperty="Tabs")]
    [ToolboxData("<{0}:TabControl runat\"server\"></{0}:TabControl>")]
    [Serializable()]
    public class TabControl : System.Web.UI.WebControls.Panel
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (ViewState["Text"] == null) return "";
                return (string)ViewState["Text"];
            }

            set
            {
                ViewState["Text"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string AjaxOptions
        {
            get
            {
                if (ViewState["AjaxOptions"] == null) return "";
                return (string)ViewState["AjaxOptions"];
            }

            set
            {
                ViewState["AjaxOptions"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Cache
        {
            get
            {
                if (ViewState["Cache"] == null) return false;
                return (bool)ViewState["Cache"];
            }

            set
            {
                ViewState["Cache"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Collapsible
        {
            get
            {
                if (ViewState["Collapsible"] == null) return false;
                return (bool)ViewState["Collapsible"];
            }

            set
            {
                ViewState["Collapsible"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Cookie
        {
            get
            {
                if (ViewState["Cookie"] == null) return "";
                return (string)ViewState["Cookie"];
            }

            set
            {
                ViewState["Cookie"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Deselectable
        {
            get
            {
                if (ViewState["Deselectable"] == null) return false;
                return (bool)ViewState["Deselectable"];
            }

            set
            {
                ViewState["Deselectable"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Disabled
        {
            get
            {
                if (ViewState["Disabled"] == null) return "";
                return (string)ViewState["Disabled"];
            }

            set
            {
                ViewState["Disabled"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Event
        {
            get
            {
                if (ViewState["Event"] == null) return "";
                return (string)ViewState["Event"];
            }

            set
            {
                ViewState["Event"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Fx
        {
            get
            {
                if (ViewState["fx"] == null) return "";
                return (string)ViewState["fx"];
            }

            set
            {
                ViewState["fx"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string IdPrefix
        {
            get
            {
                if (ViewState["IdPrefix"] == null) return "";
                return (string)ViewState["IdPrefix"];
            }

            set
            {
                ViewState["IdPrefix"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PanelTemplate
        {
            get
            {
                if (ViewState["PanelTemplate"] == null) return "";
                return (string)ViewState["PanelTemplate"];
            }

            set
            {
                ViewState["PanelTemplate"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Selected
        {
            get
            {
                if (ViewState["Selected"] == null) return 0;
                return (int)ViewState["Selected"];
            }

            set
            {
                ViewState["Selected"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Spinner
        {
            get
            {
                if (ViewState["Spinner"] == null) return "";
                return (string)ViewState["Spinner"];
            }

            set
            {
                ViewState["Spinner"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TabTemplate
        {
            get
            {
                if (ViewState["TabTemplate"] == null) return "";
                return (string)ViewState["TabTemplate"];
            }

            set
            {
                ViewState["TabTemplate"] = value;
            }
        }


        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabsSelect
        {
            get
            {
                if (ViewState["OnClientTabSelect"] == null) return "";
                return (string)ViewState["OnClientTabSelect"];
            }

            set
            {
                ViewState["OnClientTabSelect"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabsLoad
        {
            get
            {
                if (ViewState["OnClientTabsLoad"] == null) return "";
                return (string)ViewState["OnClientTabsLoad"];
            }

            set
            {
                ViewState["OnClientTabsLoad"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabsShow
        {
            get
            {
                if (ViewState["OnClientTabsShow"] == null) return "";
                return (string)ViewState["OnClientTabsShow"];
            }

            set
            {
                ViewState["OnClientTabsShow"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabsAdd
        {
            get
            {
                if (ViewState["OnClientTabsAdd"] == null) return "";
                return (string)ViewState["OnClientTabsAdd"];
            }

            set
            {
                ViewState["OnClientTabsAdd"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabsRemove
        {
            get
            {
                if (ViewState["OnClientTabsRemove"] == null) return "";
                return (string)ViewState["OnClientTabsRemove"];
            }

            set
            {
                ViewState["OnClientTabsRemove"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabsEnable
        {
            get
            {
                if (ViewState["OnClientTabsEnable"] == null) return "";
                return (string)ViewState["OnClientTabsEnable"];
            }

            set
            {
                ViewState["OnClientTabsEnable"] = value;
            }
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabsDisable
        {
            get
            {
                if (ViewState["OnClientTabsDisable"] == null) return "";
                return (string)ViewState["OnClientTabsDisable"];
            }

            set
            {
                ViewState["OnClientTabsDisable"] = value;
            }
        }
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Category("Appearance")]
        public List<Tab> Tabs
        {
            get;
            private set;
        }
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Category("Appearance")]
        public List<TabPage> TabPages
        {
            get;
            private set;
        }

        public TabControl()
        {
            Tabs = new List<Tab>();
            TabPages = new List<TabPage>();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<div id=" + this.ClientID + "><ul>");

            foreach (Tab c in this.Tabs)
            {
                if (c.TabName != null && c.TabName != "")
                {
                    if(c.Page==null) c.Page = this.Page;
                    c.RenderControl(writer);
                }
            }
            
            writer.Write("</ul>");
            foreach (TabPage c in this.TabPages)
            {
                if (c.TabName != null && c.TabName != "")
                {
                    if (c.Page == null) c.Page = this.Page;
                    c.RenderControl(writer);
                }
            }
            writer.Write("</div>");


            writer.Write("<script type=\"text/javascript\">");
            writer.Write("jQuery(document).ready(function($) { var ctl=$(\"#" + this.ClientID + "\"); ctl.tabs({");

            bool needsComma = false;
            if (AjaxOptions != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("ajaxOptions: {" + AjaxOptions + "}");
                needsComma = true;
            }
            if (Cache)
            {
                if (needsComma) writer.Write(",");
                writer.Write("cace: true");
                needsComma = true;
            }
            if (Collapsible)
            {
                if (needsComma) writer.Write(",");
                writer.Write("collapsible: true");
                needsComma = true;
            }
            if (Deselectable)
            {
                if (needsComma) writer.Write(",");
                writer.Write("deselectable: true");
                needsComma = true;
            }
            if (Disabled != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("disabled: [" + Disabled + "]");
                needsComma = true;
            }
            if (Event !="")
            {
                if (needsComma) writer.Write(",");
                writer.Write("event: '" + Event + "'");
                needsComma = true;
            }
            if (Fx!="")
            {
                if (needsComma) writer.Write(",");
                writer.Write("fx: " + Fx);
                needsComma = true;
            }
            if (IdPrefix!="")
            {
                if (needsComma) writer.Write(",");
                writer.Write("idPrefix: '" + IdPrefix + "'");
                needsComma = true;
            }
            if (PanelTemplate != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("panelTemplate: '" + IdPrefix + "'");
                needsComma = true;
            }
            if (Selected != 0)
            {
                if (needsComma) writer.Write(",");
                writer.Write("selected: " + Selected + "");
                needsComma = true;
            }
            if (Spinner != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("spinner: '" + Spinner + "'");
                needsComma = true;
            }
            if (TabTemplate != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("tabTemplate: '" + TabTemplate + "'");
                needsComma = true;
            }
            if (OnClientTabsSelect != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("select: function(event, ui) {" + OnClientTabsSelect + "}");
                needsComma = true;
            }
            if (OnClientTabsLoad != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("load: function(event, ui) {" + OnClientTabsLoad + "}");
                needsComma = true;
            }
            if (OnClientTabsShow != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("show: function(event, ui) {" + OnClientTabsShow + "}");
                needsComma = true;
            }
            if (OnClientTabsAdd != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("add: function(event, ui) {" + OnClientTabsAdd + "}");
                needsComma = true;
            }
            if (OnClientTabsRemove != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("remove: function(event, ui) {" + OnClientTabsRemove + "}");
                needsComma = true;
            }
            if (OnClientTabsEnable != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("enable: function(event, ui) {" + OnClientTabsEnable + "}");
                needsComma = true;
            }
            if (OnClientTabsDisable != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("disable: function(event, ui) {" + OnClientTabsDisable + "}");
                needsComma = true;
            }

            writer.Write("});});");
            writer.Write("</script>");
        }
    }
}